package com.ruoyi.web.controller.community.cpc;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.community.domain.BaseActivityRegisterRequest;
import com.ruoyi.community.domain.CpcActivity;
import com.ruoyi.community.service.ICpcActivityEnrollService;
import com.ruoyi.community.service.ICpcActivityParticipateService;
import com.ruoyi.community.service.ICpcActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 党建活动Controller
 * 
 * @author ruoyi
 * @date 2021-08-28
 */
@RestController
@RequestMapping("/community/cpc/activity")
public class CpcActivityController extends BaseController
{
    @Autowired
    private ICpcActivityService CpcActivityService;

    @Autowired
    private ICpcActivityEnrollService CpcActivityEnrollService;

    @Autowired
    private ICpcActivityParticipateService CpcActivityParticipateService;

    /**
     * 查询党建活动
     */
//    @PreAuthorize("@ss.hasPermi('community:activity:list')")
    @GetMapping("/list")
    public TableDataInfo list(CpcActivity CpcActivity)
    {
        startPage();
        List<CpcActivity> list = CpcActivityService.selectCpcActivityList(CpcActivity);
        return getDataTable(list);
    }

    /**
     * 导出党建活动
     */
//    @PreAuthorize("@ss.hasPermi('community:activity:export')")
    @Log(title = "党建活动", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CpcActivity CpcActivity)
    {
        List<CpcActivity> list = CpcActivityService.selectCpcActivityList(CpcActivity);
        ExcelUtil<CpcActivity> util = new ExcelUtil<CpcActivity>(CpcActivity.class);
        return util.exportExcel(list, "党建活动数据");
    }

    /**
     * 获取党建活动详细信息
     */
//    @PreAuthorize("@ss.hasPermi('community:activity:query')")
    @GetMapping(value = "/{activityId}")
    public AjaxResult getInfo(@PathVariable("activityId") String activityId)
    {
        return AjaxResult.success(CpcActivityService.selectCpcActivityById(activityId));
    }

    /**
     * 新增党建活动
     */
//    @PreAuthorize("@ss.hasPermi('community:activity:add')")
    @Log(title = "党建活动", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CpcActivity CpcActivity)
    {
        return toAjax(CpcActivityService.insertCpcActivity(CpcActivity));
    }

    /**
     * 修改党建活动
     */
//    @PreAuthorize("@ss.hasPermi('community:activity:edit')")
    @Log(title = "党建活动", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CpcActivity CpcActivity)
    {
        return toAjax(CpcActivityService.updateCpcActivity(CpcActivity));
    }

    /**
     * 删除党建活动
     */
//    @PreAuthorize("@ss.hasPermi('community:activity:remove')")
    @Log(title = "党建活动", businessType = BusinessType.DELETE)
	@DeleteMapping("/{activityIds}")
    public AjaxResult remove(@PathVariable String[] activityIds)
    {
        return toAjax(CpcActivityService.deleteCpcActivityByIds(activityIds));
    }

    /**
     * 查询是否已签到
     */
    @PostMapping("/state")
    public int ifEnrolledOrRegistered(@RequestBody BaseActivityRegisterRequest baseActivityRegisterRequest)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        baseActivityRegisterRequest.setUserId(loginUser.getUserId());
        if(!CpcActivityEnrollService.hasEnrolled(baseActivityRegisterRequest)){
            return 0;
        }else if(!CpcActivityParticipateService.hasRegistered(baseActivityRegisterRequest)){
            return 1;
        }else {
            return 2;
        }
    }

//    /**
//     * 查询用户历史记录,包括投票、报名党建活动和参与党建活动
//     */
//    @GetMapping("/me")
//    public AjaxResult getMyRecord()
//    {
//        return AjaxResult.success(CpcActivityService.getMyRecord());
//    }
}
